const { exec } = require("child_process");
const Router = require("koa-router");
const fs = require("fs"); // 引入fs模块
const router = new Router({ prefix: "/ros" });

router.post("/roslaunch", async (ctx) => {
    try {
        // 激活工作空间
        await exec("source /home/cs/test_ws/devel/setup.bash");
    
        // 执行rosrun命令
        const { command, args } = ctx.request.body; // 假设请求体包含 command 和 args
        const fullCommand = `${command} ${args.join(" ")}`; // 根据提供的 args 构建完整命令

        await exec(fullCommand);
        console.log(fullCommand);
        ctx.body = {
        success: true,
        message: "rosrun command executed successfully.",
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
        success: false,
        error: error.message,
        };
    }
});

module.exports = router;